package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.cache.J2CacheUtils;
import com.sugon.dao.SparePartOutDao;
import com.sugon.entity.SparePartOutEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.service.SparePartOutService;
import com.sugon.service.SysMsgService;
import com.sugon.utils.Constant;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.SysMsgType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 出库记录Service实现类
 *
 * @author sugon
 * @date 2019-07-12 08:50:23
 */
@Service("sparePartOutService")
public class SparePartOutServiceImpl implements SparePartOutService {
    @Resource
    private SparePartOutDao sparePartOutDao;
    @Resource
    private SysMsgService sysMsgService;

    /**
     * statTotalNum
     *
     * @param posId
     * @param month
     * @return
     */
    @Override
    public Integer statTotalNum(String posId, String month) {
        Integer totalNum = sparePartOutDao.statTotalNum(posId, month);
        return totalNum == null ? 0 : totalNum;
    }

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public SparePartOutEntity queryObject(String id) {
        return sparePartOutDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<SparePartOutEntity> queryList(Map<String, Object> map) {
        return sparePartOutDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return sparePartOutDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param sparePartOut 实体
     * @return
     */
    @Override
    public int save(SparePartOutEntity sparePartOut) {
        sparePartOut.setId(RandomUtil.randomString(32));
        sparePartOut.setOutCode(J2CacheUtils.getSequence(null, Constant.SPARE_OUT));
        sparePartOut.setRelNum(sparePartOut.getOutNum());
        sparePartOut.setBackNum(0);
        sparePartOut.setDelFlag(Constant.IS_NOT_DEL_INT);
        if (StrUtil.isEmpty(sparePartOut.getCreateUser())) {
            sparePartOut.setCreateUser(getUserId());
        }
        sparePartOut.setCreateDate(DateUtil.date());
        return sparePartOutDao.save(sparePartOut);
    }

    /**
     * update
     *
     * @param sparePartOut 实体
     * @return
     */
    @Override
    public int update(SparePartOutEntity sparePartOut) {
        return sparePartOutDao.update(sparePartOut);
    }

    /**
     * review
     *
     * @param sparePartOut
     * @return
     */
    @Override
    public int review(SparePartOutEntity sparePartOut) {
        // 添加审核 消息提醒
        // sysProcessInfoService.saveProcessInfo(sparePartOut.getId(), ProcessInfoEnum.DEV_SBQD,sparePartOut.getCheckStatus(),null);
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        sysMsgEntity.setDataId(sparePartOut.getId());
        sysMsgEntity.setDataType(ProcessInfoEnum.SPA_CKSP);
        sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
        sysMsgService.allMsgData(sysMsgEntity);
        return sparePartOutDao.update(sparePartOut);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return sparePartOutDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return sparePartOutDao.deleteBatch(ids);
    }

    /**
     * deleteRecord
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteRecord(String ids) {
        SparePartOutEntity sparePartOut = sparePartOutDao.queryObject(ids);
        sparePartOut.setDelFlag(Integer.valueOf(Constant.IS_DEL));
        return sparePartOutDao.update(sparePartOut);
    }
}
